package com.ntty.utils;

import java.net.InetSocketAddress;
import java.net.SocketAddress;



import hyl.core.MyDate;
import hyl.core.MyFun;
import hyl.core.conf.MyConst;
import io.netty.channel.ChannelHandlerContext;

public class FileTools {
	private static String FILESAVEPATH = null;

	public static String getUploadPath() {
		// req.getServletContext().getRealPath("/upload");
		if (FILESAVEPATH == null) {
			FILESAVEPATH = MyConst.getInstance().get("UPLOADPATH").trim() + "/";
		}
		return FILESAVEPATH;
	}

	static String baseurl;

	public static String getBasePath(ChannelHandlerContext ctx) {
		InetSocketAddress sa = (InetSocketAddress) ctx.channel().localAddress();
		if (baseurl == null) {
			baseurl = "http://" + sa.getHostName() + ":" + sa.getPort();
		}
		return baseurl;
	}

	public static String getPath(String dir) {
		return dir + MyDate.getNow("/yyyy/MM/dd/");
	}

	public static String getExtName(String oldname) {
		if (MyFun.isEmpty(oldname))
			return null;
		return oldname.substring(oldname.lastIndexOf("."));
	}

	public static String getNewName(String filename) {
		return MyFun.getUUID() + getExtName(filename);
	}
}
